package com.foodchain.service;

import com.foodchain.entity.UserPointsConsume;
import com.foodchain.entity.UserPointsGain;
import com.foodchain.repository.UserPointsConsumeRepository;
import com.foodchain.repository.UserPointsGainRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.*;

/**
 * @ClassName UserPointsGainService
 * @Description TODO
 * @Author duyuanyuan
 * @Date 2019/3/27 13:57
 * @Version 1.0
 **/
@Service
public class UserPointsGainService {
    @Resource
    private UserPointsGainRepository repository;

    @Resource
    private UserPointsConsumeRepository consumeRepository;

    public Page<UserPointsGain> findGainList(Integer page, Integer size, String userId) {
        Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, "gainDate");
        Specification<UserPointsGain> spec = new Specification<UserPointsGain>() {        //查询条件构造
            public Predicate toPredicate(Root<UserPointsGain> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<Boolean> userid= root.get("userId");
                Predicate p1 = cb.equal(userid,userId);
                Predicate p = cb.and(p1);
                return p;
            }
        };
        return   repository.findAll(spec,pageable);
    }

    public Page<UserPointsConsume> findConsumeList(Integer page, Integer size, String userId) {
        Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, "consumeDate");
        Specification<UserPointsConsume> spec = new Specification<UserPointsConsume>() {        //查询条件构造
            public Predicate toPredicate(Root<UserPointsConsume> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<Boolean> userid= root.get("userId");
                Predicate p1 = cb.equal(userid,userId);
                Predicate p = cb.and(p1);
                return p;
            }
        };
        return   consumeRepository.findAll(spec,pageable);
    }
}
